#include "groupsessiondetailwidget.h"
#include "sessiondetailwidget.h"
#include "debug.h"

/******************************
 * 群聊会话详情窗口
 *****************************/
GroupsessionDetailWidget::GroupsessionDetailWidget(QWidget *parent)
    :QDialog(parent)
{
    // 1. 设置窗口的基本属性.
    this->setFixedSize(320,500);
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    // 窗口被关闭时, 自动销毁这个对话框对象.
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(10,10,10,10);
    this->setLayout(vlayout);

    // 3. 创建滚动区域
    // 3.1 创建 QScrollArea 对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    // 设置滚动条相关的样式
    QString style = "QScrollBar:vertical {border: none;background: #FBFBFB;width: 10px;margin: 0px 0px 0px 0px;}\
        QScrollBar::handle:vertical {background: #c1c1c1;min-height: 20px;border-radius: 5px;}\
        QScrollBar::handle:vertical:hover {background: #a8a8a8;}\
        QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {height: 0px;}\
        QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {background: none;}";
    scrollArea->verticalScrollBar()->setStyleSheet(style);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollArea { border:none; }");
    scrollArea->setFixedSize(300,350);
    scrollArea->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    // 3.2 创建一个 QScrollArea 内部的 QWidget
    QWidget* scrollWidget = new QWidget();
    scrollArea->setWidget(scrollWidget);

    // 3.3 给 scrollWidget 里面添加一个 网格布局
    glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(0,0,0,0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    scrollWidget->setLayout(glayout);

    // 3.4 把滚动区域, 添加到布局管理器中
    vlayout->addWidget(scrollArea);

    // 4. 添加 "添加按钮"
    AvatarItem* addItem = new AvatarItem("添加",QIcon(":/resource/image/cross.png"));
    glayout->addWidget(addItem);

    // 5. 添加 "群聊名称"
    QLabel* groupTag = new QLabel();
    groupTag->setText("群聊名称");
    groupTag->setStyleSheet("QLabel {font-weight: 700; font-size: 16px;}");
    groupTag->setFixedHeight(20);
    groupTag->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    // 设置文字在 QLabel 内部的对齐方式.
    groupTag->setAlignment(Qt::AlignBottom);
    vlayout->addWidget(groupTag,0,Qt::AlignLeft | Qt::AlignVCenter);

    // 6. 添加 真实的群聊名字 和 修改按钮
    // 6.1 创建水平布局
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);
    vlayout->addLayout(hlayout);

    // 6.2 创建真实群聊名字的 label
    groupLabel = new QLabel();
    groupLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
    groupLabel->setFixedHeight(20);
    groupLabel->setStyleSheet("QLabel { font-size: 18px; }");
    hlayout->addWidget(groupLabel,0,Qt::AlignLeft | Qt::AlignVCenter);

    // 6.3 创建 "修改按钮"
    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(30,30);
    modifyBtn->setIconSize(QSize(30,30));
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(230, 230, 230); }");
    hlayout->addWidget(modifyBtn,0,Qt::AlignRight | Qt::AlignVCenter);

    // 7. 退出群聊按钮
    QPushButton* exitBtn = new QPushButton();
    exitBtn->setFixedHeight(50);
    exitBtn->setText("退出群聊");
    exitBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; background-color: transparent;}";
    btnStyle += "QPushButton:pressed { background-color: rgb(255, 0, 0); }";
    exitBtn->setStyleSheet(btnStyle);
    vlayout->addWidget(exitBtn);

    // 此处构造假的数据用来测试界面
#if TEST_UI
    groupLabel->setText("人类吃喝行为研究小组");
    QIcon icon(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;i++)
    {
        AvatarItem* avatarItem = new AvatarItem("小短腿",icon);
        this->addMerber(avatarItem);
    }
#endif

}

void GroupsessionDetailWidget::addMerber(AvatarItem *avatarItem)
{
    // 拿到滚动区域中的布局管理器, 取到成员变量即可.
    const int MAX_COL = 4;
    if(curCol >= MAX_COL)
    {
        // 换行操作
        ++curRow;
        curCol = 0;
    }
    glayout->addWidget(avatarItem,curRow,curCol);
    ++curCol;
}

void GroupsessionDetailWidget::leaveEvent(QEvent *event)
{
    (void) event;
    this->close();
    QDialog::leaveEvent(event);
}

void GroupsessionDetailWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(rect(), 15, 15);  // 圆角半径
    painter.fillPath(path, QColor("#F3F3F3"));
}
